package dev.browse;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

class WebToolBar extends JToolBar implements HyperlinkListener {
	private static final long serialVersionUID = 468957650446720618L;

	private WebBrowserPane webBrowserPane;
	private JButton backButton;
	private JButton forwardButton;
	private JTextField urlTextField;
	private JPanel panel;
	
	
	public WebToolBar(WebBrowserPane browser, JPanel panel) {
		this(browser);
		this.panel = panel;
	}
	
	public WebToolBar(WebBrowserPane browser) {
		super("Web Navigation");

		webBrowserPane = browser;
		webBrowserPane.addHyperlinkListener(this);

		urlTextField = new JTextField(25);
		urlTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				try {
					String sUrl = urlTextField.getText();
					URL url = new URL(Html.toURL(sUrl));
					webBrowserPane.goToURL(url);
				}catch (MalformedURLException urlException) {
					urlException.printStackTrace();
				}
			}
		});

		backButton = new JButton("back");
		backButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				URL url = webBrowserPane.back();
				urlTextField.setText(url.toString());
			}
		});

		forwardButton = new JButton("forward");

		forwardButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				URL url = webBrowserPane.forward();
				urlTextField.setText(url.toString());
			}
		});
		add(backButton);
		add(forwardButton);
		add(urlTextField);
	}

	public void hyperlinkUpdate(HyperlinkEvent event) {
		if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
			URL url = event.getURL();
			webBrowserPane.goToURL(url);
			urlTextField.setText(url.toString());
		}
	}
}
